package com.xiweicheng.test.springboot.jpa;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.AuditorAware;
import org.springframework.stereotype.Component;

@Component
public class CustomAuditorAware implements AuditorAware<User> {

	@Autowired
	UserRepository userRepository;

	User user;

	@Override
	public User getCurrentAuditor() {

		if (user == null) {
			List<User> users = userRepository.findAll();
			user = users.isEmpty() ? userRepository.saveAndFlush(User.builder().name("uname").build()) : users.get(0);
		}

		return user;

	}
}
